package com.app.hmst.main;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.app.hmst.dto.Category;
import com.app.hmst.dto.Forms;
import com.app.hmst.dto.Homestay;
import com.app.hmst.dto.HomestayDto;
import com.app.hmst.dto.House;
import com.app.hmst.dto.HouseDto;
import com.app.hmst.dto.Indicators;
import com.app.hmst.dto.Sub_categories;
import com.app.hmst.json.JSONParserForGetList;
import com.app.hmst.model.CategoryVO;
import com.app.hmst.model.HomestayInfoVO;
import com.app.hmst.model.HomestayVO;
import com.app.hmst.model.HouseInfoVO;
import com.app.hmst.model.IndicatorVO;
import com.app.hmst.model.SubCategoryVO;
import com.app.hmst.utils.GlobalVariable;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.roscopeco.ormdroid.Entity;

@SuppressLint({ "NewApi", "ValidFragment" }) public class HomePageFragment extends Fragment{
	private View v;
	private ImageView menu_icon,start_evaluate,viewAllEvaluate,clickToNoti;
	private SlidingMenu _sm;
	private Fragment mContent;
	
	public HomePageFragment(SlidingMenu sm){
		this._sm = sm;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		
		v = inflater.inflate(R.layout.fragment_home_main_page,container, false);
		start_evaluate =(ImageView) v.findViewById(R.id.imageView3);
		menu_icon = (ImageView) v.findViewById(R.id.imageView6);
		viewAllEvaluate = (ImageView) v.findViewById(R.id.imageView4);
		clickToNoti = (ImageView) v.findViewById(R.id.imageView9);
		clickToNoti.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new ViewAllEvaluatePageFragment(_sm));
			}
		});
		
		//Execute background process
		new InitAndLoadData().execute();

		viewAllEvaluate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new ViewAllEvaluatePageFragment(_sm));
			}
		});
		menu_icon.setVisibility(View.INVISIBLE);
//		menu_icon.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				_sm.toggle();
//			}
//		});
		start_evaluate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new StartEvaluateFragment(_sm));
				
			}
		});
		  SharedPreferences.Editor editor = getActivity().getSharedPreferences("MyPref", 0).edit();
          editor.putBoolean("isLogin", false);
          editor.commit();
		new Utils(getActivity()).onBackPressed(v, null,getActivity());
		return v;
	}

	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	public class InitAndLoadData extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub			
			List<HomestayInfoVO> homestayInfoVOs = Entity.query(HomestayInfoVO.class).executeMulti();
			if(homestayInfoVOs==null || homestayInfoVOs.isEmpty()){
				List<HomestayDto> homestayDtos = JSONParserForGetList.getInstance().getHomeStays();
				if(homestayDtos!=null && homestayDtos.size()>0){
					for(HomestayDto dto : homestayDtos){
						HomestayInfoVO homestayInfoVO = new HomestayInfoVO();
						homestayInfoVO.id = Integer.parseInt(dto.getId());
						homestayInfoVO.name = dto.getName();
						homestayInfoVO.province = dto.getProvince();
						homestayInfoVO.district = dto.getDistrict();
						homestayInfoVO.tumbol = dto.getTumbol();
						homestayInfoVO.address = dto.getAddress();
						homestayInfoVO.tel = dto.getTel();
						homestayInfoVO.mobile = dto.getMobile();
						homestayInfoVO.fax = dto.getFax();
						homestayInfoVO.email = dto.getEmail();
						homestayInfoVO.website = dto.getWebsite();
						homestayInfoVO.contactName = dto.getContactName();
						homestayInfoVO.sumCode = dto.getSumCode();
						homestayInfoVO.status = dto.getStatus();
						
						if(dto.getHouse()!=null && dto.getHouse().size()>0){
							for(HouseDto hDto : dto.getHouse()){
								HouseInfoVO houseInfoVO = new HouseInfoVO();
								houseInfoVO.id = Integer.parseInt(hDto.getId());
								houseInfoVO.name = hDto.getName();
								houseInfoVO.contactName = hDto.getContactName();
								houseInfoVO.tel= hDto.getTel();
								houseInfoVO.fax= hDto.getFax();
								houseInfoVO.email= hDto.getEmail();
								houseInfoVO.website= hDto.getWebsite();
								houseInfoVO.homestayId = dto.getId();
								houseInfoVO.save();
							}
						}
						homestayInfoVO.save();
						
					}
				}
			}
			List<HomestayVO> homestayVOs = Entity.query(HomestayVO.class).executeMulti();
			if(homestayVOs==null || homestayVOs.isEmpty()){
				Forms forms = JSONParserForGetList.getInstance().getForms();
				if(forms!=null){
					Homestay homestay = forms.getHomestay();
					if(homestay!=null){
						HomestayVO homestayVO = new HomestayVO();
						homestayVO.id =  Integer.parseInt(homestay.getId());
						homestayVO.title = homestay.getTitle();
						homestayVO.type = GlobalVariable.TYPE_HOMESTAY;
						homestayVO.save();
						List<Category> catList = homestay.getCategory();
						if(catList!=null && catList.size()>0){
							for(Category cat : catList){
								CategoryVO catVO = new CategoryVO();
								catVO.id = Integer.parseInt(cat.getId());
								catVO.no = cat.getNo();
								catVO.title = cat.getTitle();
								catVO.homestayId  = homestay.getId();
								catVO.save();
								List<Sub_categories> subList = cat.getSub_categories();
								
								if(subList!=null && subList.size()>0){
									for(Sub_categories sub : subList){
										SubCategoryVO subCategoryVO = new SubCategoryVO();
										subCategoryVO.id = Integer.parseInt(sub.getId());
										subCategoryVO.no = sub.getNo();
										subCategoryVO.title = sub.getTitle();
										subCategoryVO.categoryId = cat.getId();
										subCategoryVO.save(); 
										List<Indicators> indicators = sub.getIndicators();
										if(indicators!=null && indicators.size()>0){
											for(Indicators in : indicators){
												IndicatorVO indicatorVO = new IndicatorVO();
												indicatorVO.id = Integer.parseInt(in.getId());
												indicatorVO.isCamera = in.getIsCamera() ? "T" : "F";
												indicatorVO.isDocument = in.getIsDocument() ? "T" : "F";
												indicatorVO.isInterview = in.getIsInterview() ? "T" : "F";
												indicatorVO.no = in.getNo();
												indicatorVO.notice = in.getNotice();
												indicatorVO.title = in.getTitle();
												indicatorVO.subCategoryId = sub.getId();
												indicatorVO.save();
											}
										}
										
									}
								}
								
							}
						}
						
					}
					House house = forms.getHouse();
					if(house!=null){
						HomestayVO homestayVO = new HomestayVO();
						homestayVO.id =  Integer.parseInt(house.getId());
						homestayVO.title = house.getTitle();
						homestayVO.type = GlobalVariable.TYPE_HOUSE;
						homestayVO.save();
						List<Category> catList = house.getCategory();
						if(catList!=null && catList.size()>0){
							for(Category cat : catList){
								CategoryVO catVO = new CategoryVO();
								catVO.id = Integer.parseInt(cat.getId());
								catVO.no = cat.getNo();
								catVO.title = cat.getTitle();
								catVO.homestayId  = house.getId();
								catVO.save();
								List<Sub_categories> subList = cat.getSub_categories();
								
								if(subList!=null && subList.size()>0){
									for(Sub_categories sub : subList){
										SubCategoryVO subCategoryVO = new SubCategoryVO();
										subCategoryVO.id = Integer.parseInt(sub.getId());
										subCategoryVO.no = sub.getNo();
										subCategoryVO.title = sub.getTitle();
										subCategoryVO.categoryId = cat.getId();
										subCategoryVO.save();
										List<Indicators> indicators = sub.getIndicators();
										if(indicators!=null && indicators.size()>0){
											for(Indicators in : indicators){
												IndicatorVO indicatorVO = new IndicatorVO();
												indicatorVO.id = Integer.parseInt(in.getId());
												indicatorVO.isCamera = in.getIsCamera() ? "T" : "F";
												indicatorVO.isDocument = in.getIsDocument() ? "T" : "F";
												indicatorVO.isInterview = in.getIsInterview() ? "T" : "F";
												indicatorVO.no = in.getNo();
												indicatorVO.notice = in.getNotice();
												indicatorVO.title = in.getTitle();
												indicatorVO.subCategoryId = sub.getId();
												indicatorVO.save();
											}
										}
										
									}
								}
								
							}
						}
					}
				}
		}
			return null;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
}
